﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMD.DataAccess.Interface.Dao;
using SmartMD.DataModel.Entity;
using System.Data;
using System.Collections.ObjectModel;

namespace SmartMD.DataAccess.Oracle.DaoImpl
{
    public class IndiceDefineDaoImpl : BaseGenericDaoImpl, IIndiceDefineDao
    {
        public bool DeleteObject(Object entity)
        {
            IndiceDefine indice = (IndiceDefine)entity;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" delete from DUO_INDICE_DEFINE");
            sb.Append(" where ID ='" + indice.Id + "'");

            bool success = DBUtil.Delete(sb.ToString());
            return success;
        }

        public bool SaveOrUpdateObject(Object entity)
        {
            //删除对象
            DeleteObject(entity);

            IndiceDefine indice = (IndiceDefine)entity;
            StringBuilder sb = new StringBuilder();

            sb.Clear();
            sb.Append(" insert into");
            sb.Append(" DUO_INDICE_DEFINE (ID, ITEMID, NAME, UNIT, NORMALVALUE, COLUMNNUM, COLUMNTYPE)");
            sb.Append("  values(");
            sb.Append(" '" + indice.Id + "',");
            sb.Append(" '" + indice.ItemId + "',");
            sb.Append(" '" + indice.Name + "',");
            sb.Append(" '" + indice.Unit + "',");
            sb.Append(" '" + indice.NormalValue + "',");
            sb.Append(" " + indice.ColumnNo + ",");
            sb.Append(" 'string'");
            sb.Append(")");
            bool isSaved = this.dbConn.Insert(sb.ToString());
            return isSaved;
        }
        /**
        * 
        * <p>功能描述：保存指标定义数组</p>
        * <p>修改人:</p>
        * <p>修改时间:</p>
        * <p>修改描述:</p>
        * @param id
        * @return
        */
        public bool saveIndiceDefines(List<IndiceDefine> indiceDefines)
        {
            bool isSaved = true;
            IndiceDefine indice;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indiceDefines.Count; i++)
            {
                indice = indiceDefines[i];
                bool success = SaveOrUpdateObject(indice);
               isSaved = isSaved & success;
            }
            return isSaved;
        }

        /**
        * 
        * <p>功能描述：保存指标定义数组</p>
        * <p>修改人:</p>
        * <p>修改时间:</p>
        * <p>修改描述:</p>
        * @param id
        * @return
        */
        public List<IndiceDefine> loadAllIndiceDefines()
        {
            throw new NotImplementedException();
        }

        /**
        * 
        * <p>功能描述：保存指标定义数组</p>
        * <p>修改人:</p>
        * <p>修改时间:</p>
        * <p>修改描述:</p>
        * @param id
        * @return
        */
        public ObservableCollection<IndiceDefine> loadIndiceDefineById(string itemId)
        {
            if (itemId == null || itemId == "")
                return null;
            ObservableCollection<IndiceDefine> indiceDefineList = new ObservableCollection<IndiceDefine>();
            IndiceDefine indiceDefine;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_INDICE_DEFINE");
            sb.Append(" where ITEMID ='" + itemId + "'");
            sb.Append("  order by COLUMNNUM ASC");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    indiceDefine = new IndiceDefine();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        indiceDefine.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    indiceDefineList.Add(indiceDefine);
                }
                return indiceDefineList;
            }
            else
                return null;     
        }
    }
}
